package cur.nature.component.minio.config;

import io.minio.GetObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.ListObjectsArgs;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Item;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class StorageClient {

    /**
     * @param fileName 文件名称
     * @param time     时间
     * @Description 获取上传临时签名
     */
    public static Map getPolicy(String fileName, ZonedDateTime time) {
        MinioConfig config = MinioClients.getConfiguration();
        return MinioClients.getPolicy(config.getBucketId(), fileName, config.getFilePath(), time);
    }

    /**
     * @param objectName 对象名称
     * @param method     方法
     * @param time       时间
     * @param timeUnit   时间单位
     * @Description 获取上传文件的url
     */
    public static String getPolicyUrl(String fileName,  Method method, int time, TimeUnit timeUnit) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioConfig config = MinioClients.getConfiguration();
        return MinioClients.getPolicyUrl(config.getBucketId(), fileName, config.getFilePath(), method, time, timeUnit);
    }

    /**
     * @param file     文件
     * @param fileName 文件名称
     * @Description 上传文件
     */
    public static void upload(String fileName, MultipartFile file) throws Exception {
        MinioConfig config = MinioClients.getConfiguration();
        MinioClients.upload(config.getBucketId(),fileName,config.getFilePath(),file);
    }

    /**
     * @Description description: 下载文件
     */
    public static void download(String fileName, HttpServletResponse response) throws Exception {
        MinioConfig config = MinioClients.getConfiguration();
        MinioClients.download(config.getBucketId(),fileName,config.getFilePath(),response);
    }

    /**
     * @param objectName 对象名称
     * @param time       时间
     * @param timeUnit   时间单位
     * @Description 根据filename获取文件访问地址
     */
    public static String getUrl(String objectName, int time, TimeUnit timeUnit) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioConfig config = MinioClients.getConfiguration();
        return MinioClients.getUrl(config.getBucketId(),objectName,config.getFilePath(),time,timeUnit);
    }

    /**
     * @param objectFile 对象文件
     */
    public static String getFileUrl(String fileName) {
        MinioConfig config = MinioClients.getConfiguration();
        return MinioClients.getFileUrl(config.getBucketId(),fileName,config.getFilePath());
    }

    /**
     * 删除文件
     *
     * @param bucketName 库名称
     * @param fileName   文件名称
     * @return
     */
    public static void removeObject(String fileName) throws Exception {
        MinioConfig config = MinioClients.getConfiguration();
        MinioClients.removeObject(config.getBucketId(),fileName,config.getFilePath());
    }




}

